package cn.sunchis.demo.es;

import java.util.Map;

import javax.annotation.Resource;

import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Controller;

import cn.sunchis.demo.es.model.Book;
import cn.sunchis.demo.es.repository.BookRepository;

/**
 * 
 * @author ccpg
 * @Create 2018-6-8 16:42:35
 */
@Controller
@SpringBootApplication
public class ElasticSearchDemoApplication implements CommandLineRunner {
	@Resource
    private BookRepository bookRepository;
	@Autowired
    private ElasticsearchOperations elasticsearchOperations;
	
	public static void main(String[] args) {
		SpringApplication.run(ElasticSearchDemoApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		printElasticSearchInfo();
		
		System.err.println(bookRepository.save(new Book("1001", "这是一个Spring Boot1.0的书籍", "Rambabu Posa", "23-FEB-2017")));
		bookRepository.save(new Book("1002", "Apache的一本书籍", "Rambabu Posa", "13-MAR-2017"));
		bookRepository.save(new Book("1003", "Solr是一个搜索引擎", "Rambabu Posa", "21-MAR-2017"));

//      Page<Book> books = bookService.findByTitle("书", new PageRequest(0, 10));
//
//      books.forEach(x -> System.out.println(x));
	}
	
	private void printElasticSearchInfo() {
		System.out.println("--ElasticSearch-->");
		Client client = elasticsearchOperations.getClient();
        Map<String, String> asMap = client.settings().getAsMap();
        asMap.forEach((k, v) -> {
            System.out.println(k + " = " + v);
        });
        System.out.println("<--ElasticSearch--");
	}
}
